<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="店主ID" prop="storekeeperId">
        <el-input v-model="dataForm.storekeeperId" placeholder="店主ID"></el-input>
      </el-form-item>
      <el-form-item label="所属分类ID" prop="classifyId">
        <el-input v-model="dataForm.classifyId" placeholder="所属分类ID"></el-input>
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input v-model="dataForm.storeName" placeholder="店铺名称"></el-input>
      </el-form-item>
      <el-form-item label="店铺描述" prop="storeDesc">
        <el-input v-model="dataForm.storeDesc" placeholder="店铺描述"></el-input>
      </el-form-item>
      <el-form-item label="店铺公告" prop="storeAnno">
        <el-input v-model="dataForm.storeAnno" placeholder="店铺公告"></el-input>
      </el-form-item>
      <el-form-item label="店铺头像" prop="storeIcon">
        <!-- <el-input v-model="dataForm.storeIcon" placeholder="店铺头像"></el-input> -->
        <single-upload v-model="dataForm.storeIcon"></single-upload>
      </el-form-item>
      <el-form-item label="店铺背景图" prop="backgroundUrl">
        <!-- <el-input v-model="dataForm.backgroundUrl" placeholder="店铺背景图"></el-input> -->
        <single-upload v-model="dataForm.backgroundUrl"></single-upload>
      </el-form-item>
      <el-form-item label="店铺所在区域" prop="storeLocation">
        <el-input v-model="dataForm.storeLocation" placeholder="店铺所在区域"></el-input>
      </el-form-item>
      <el-form-item label="店铺详细地址" prop="storeAddress">
        <el-input v-model="dataForm.storeAddress" placeholder="店铺详细地址"></el-input>
      </el-form-item>
      <el-form-item label="店铺联系方式" prop="storePhone">
        <el-input v-model="dataForm.storePhone" placeholder="店铺联系方式"></el-input>
      </el-form-item>
      <el-form-item label="店铺状态" prop="storeStatus">
        <el-input v-model="dataForm.storeStatus" placeholder="店铺状态"></el-input>
      </el-form-item>
      <el-form-item label="店铺诚信度" prop="storeIntegrity">
        <el-input v-model="dataForm.storeIntegrity" placeholder="店铺诚信度"></el-input>
      </el-form-item>
      <el-form-item label="营业许可证" prop="businessLicense">
        <!-- <el-input v-model="dataForm.businessLicense" placeholder="营业许可证"></el-input> -->
        <single-upload v-model="dataForm.businessLicense"></single-upload>
      </el-form-item>
      <el-form-item label="店铺招牌图" prop="storeSign">
        <!-- <el-input v-model="dataForm.storeSign" placeholder="店铺招牌图"></el-input> -->
        <single-upload v-model="dataForm.storeSign"></single-upload>
      </el-form-item>
      <el-form-item label="店铺装潢图" prop="storeFront">
        <!-- <el-input v-model="dataForm.storeFront" placeholder="店铺装潢图"></el-input> -->
        <single-upload v-model="dataForm.storeFront"></single-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        storeId: 0,
        storekeeperId: "",
        classifyId: "",
        storeName: "",
        storeDesc: "",
        storeAnno: "",
        storeIcon: "",
        backgroundUrl: "",
        storeLocation: "",
        storeAddress: "",
        storePhone: "",
        storeStatus: "",
        storeIntegrity: "",
        businessLicense: "",
        storeSign: "",
        storeFront: "",
      },
      dataRule: {
        storekeeperId: [
          { required: true, message: "店主ID不能为空", trigger: "blur" },
        ],
        classifyId: [
          { required: true, message: "所属分类ID不能为空", trigger: "blur" },
        ],
        storeName: [
          { required: true, message: "店铺名称不能为空", trigger: "blur" },
        ],
        storeDesc: [
          { required: true, message: "店铺描述不能为空", trigger: "blur" },
        ],
        storeAnno: [
          { required: true, message: "店铺公告不能为空", trigger: "blur" },
        ],
        storeIcon: [
          { required: true, message: "店铺头像不能为空", trigger: "blur" },
        ],
        backgroundUrl: [
          { required: true, message: "店铺背景图不能为空", trigger: "blur" },
        ],
        storeLocation: [
          { required: true, message: "店铺所在区域不能为空", trigger: "blur" },
        ],
        storeAddress: [
          { required: true, message: "店铺详细地址不能为空", trigger: "blur" },
        ],
        storePhone: [
          { required: true, message: "店铺联系方式不能为空", trigger: "blur" },
        ],
        storeStatus: [
          { required: true, message: "店铺状态不能为空", trigger: "blur" },
        ],
        storeIntegrity: [
          { required: true, message: "店铺诚信度不能为空", trigger: "blur" },
        ],
        businessLicense: [
          { required: true, message: "营业许可证不能为空", trigger: "blur" },
        ],
        storeSign: [
          { required: true, message: "店铺招牌图不能为空", trigger: "blur" },
        ],
        storeFront: [
          { required: true, message: "店铺装潢图不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.storeId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.storeId) {
          this.$http({
            url: this.$http.adornUrl(
              `/store/store/info/${this.dataForm.storeId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.storekeeperId = data.store.storekeeperId;
              this.dataForm.classifyId = data.store.classifyId;
              this.dataForm.storeName = data.store.storeName;
              this.dataForm.storeDesc = data.store.storeDesc;
              this.dataForm.storeAnno = data.store.storeAnno;
              this.dataForm.storeIcon = data.store.storeIcon;
              this.dataForm.backgroundUrl = data.store.backgroundUrl;
              this.dataForm.storeLocation = data.store.storeLocation;
              this.dataForm.storeAddress = data.store.storeAddress;
              this.dataForm.storePhone = data.store.storePhone;
              this.dataForm.storeStatus = data.store.storeStatus;
              this.dataForm.storeIntegrity = data.store.storeIntegrity;
              this.dataForm.businessLicense = data.store.businessLicense;
              this.dataForm.storeSign = data.store.storeSign;
              this.dataForm.storeFront = data.store.storeFront;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/store/store/${!this.dataForm.storeId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              storeId: this.dataForm.storeId || undefined,
              storekeeperId: this.dataForm.storekeeperId,
              classifyId: this.dataForm.classifyId,
              storeName: this.dataForm.storeName,
              storeDesc: this.dataForm.storeDesc,
              storeAnno: this.dataForm.storeAnno,
              storeIcon: this.dataForm.storeIcon,
              backgroundUrl: this.dataForm.backgroundUrl,
              storeLocation: this.dataForm.storeLocation,
              storeAddress: this.dataForm.storeAddress,
              storePhone: this.dataForm.storePhone,
              storeStatus: this.dataForm.storeStatus,
              storeIntegrity: this.dataForm.storeIntegrity,
              businessLicense: this.dataForm.businessLicense,
              storeSign: this.dataForm.storeSign,
              storeFront: this.dataForm.storeFront,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
